<?php

namespace slinks\extensions\action\interceptor;

use slinks\common\util\StringUtil;
use slinks\extensions\action\core\IActionInvocation;
use slinks\common\util\ObjectUtil;

/**
 * Class injects and outjects parameters from actions. 
 * 
 * @author Navid Mitchell
 *
 */
class ParameterInjectingInterceptor implements IActionInterceptor {

    private $actionParameterMapping = array();

    /**
     * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
     * request by the IActionInvocation or to short-circuit the processing and just return a String return code.
     *
     * @param IActionInvocation invocation the action invocation
     */
    public function intercept(IActionInvocation $invocation) {

        ObjectUtil::initializeWithSetters($invocation->getActionProxy()->getObject(), $invocation->getActionContext()->getValueStack()->all());

        $invocation->invoke();
    }

    /**
     * FIXME: Remove if not needed.
     *
     * @param unknown_type $class
     */
    private function buildParameterMap($class) {
        $reflectionClass = new \ReflectionClass($class);

        foreach ($reflectionClass->getMethods() as $method) {
            $methodName = $method->getName();

            if (StringUtil::beginsWith($methodName, 'set')) {
                $len = strlen($methodName);
                if ($len > 3) {
                    $var = substr($methodName, 3, $len - 3);
                    $this->actionParameterMapping[$class][] = array('var' => $var, 'method' => $methodName);
                }
            }
        }
    }

}